import cplex
import random
import numpy as np
from pylab import *
from stoch_trnsport_gen import *
from cplex.exceptions import CplexError

NI = 20
NJ = 20
NW = 20
prob,demand = gen_stoch_trnsport(NI, NJ, NW)
prob.solve()

I = range(NI)
J = range(NJ)
O = range(NW)

bigm = 0.1
print prob.solution.get_objective_value(),
print prob.solution.progress.get_num_iterations()

bigm_vec = [0.005 * i for i in range(1,100)]
bigm_arr = np.array(bigm_vec)
vals_arr = np.zeros(len(bigm_arr))

for l in range(len(bigm_vec)):
    for w in O:
        for j in J:
            bigm = bigm_vec[l]
            row_name = 'scenario' + str(w) + '_' + 'customer' + str(j)
            col_name = 'y' + '_' + str(w)
            prob.linear_constraints.set_coefficients(row_name, col_name, -bigm * demand[w][j])
            prob.linear_constraints.set_rhs(row_name, demand[w][j] * (1 - bigm) )
    prob.solve()
    vals_arr[l] = prob.solution.get_objective_value()
    print prob.solution.get_objective_value(), 
    print prob.solution.progress.get_num_iterations(),
    print bigm

plot(bigm_arr, vals_arr, '-x')
xlabel('Normalized big-M')
ylabel('Optimal cost')
grid(True)
title('|I|=|J|=|O|=20')
show()



